﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Microsoft.AspNetCore.Diagnostics;

namespace Abc.Web.Mvc.Controllers;
public class ErrorController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    //如果状态码为404，则路径将变为Error/404
    [Route("Error/{statusCode}")]
    public IActionResult HttpStatusCodeHandler(int statusCode)
    {
        switch (statusCode)
        {
            case 404:
                ViewBag.ErrorMessage = "抱歉，读者访问的页面不存在";
                break;
        }

        return View("NotFound");
    }

    [Route("Error")]
    public IActionResult Error()
    {
        //获取异常细节
        var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();

        ViewBag.ExceptionPath = exceptionHandlerPathFeature?.Path;
        ViewBag.ExceptionMessage = exceptionHandlerPathFeature?.Error.Message;
        ViewBag.StackTrace = exceptionHandlerPathFeature?.Error.StackTrace;

        return View("Error");
    }
}
